package com.bootdo.clouddo.service.user.impl;

import cn.binarywang.wx.miniapp.bean.WxMaJscode2SessionResult;
import com.bootdo.clouddo.dao.user.QaUsersMapper;
import com.bootdo.clouddo.domain.user.QaUsers;
import com.bootdo.clouddo.service.user.IUserRegisterService;
import com.bootdo.clouddo.utils.IPUtils;
import com.bootdo.clouddo.utils.RandomUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Service
public class UserRegisterServiceImpl implements IUserRegisterService {

    @Autowired
    private QaUsersMapper dao;

//    @Autowired
//    private ISendGiftService giftService;


    @Override
    public QaUsers createByWx(WxMaJscode2SessionResult sessionInfo) {

        // 构建对象
        QaUsers appUser = new QaUsers();
        appUser.setRegTime(new Date());
        appUser.setWxOpenid(sessionInfo.getOpenid());
        appUser.setWxUnionid(sessionInfo.getUnionid());
        appUser.setWxSessionKey(sessionInfo.getSessionKey());
        appUser.setRegIp(IPUtils.getIp());
        // 设置初始值
        appUser.setTotalIntegral(0d);
        appUser.setAccountIntegral(0d);
        appUser.setFrozenIntegral(0d);
        appUser.setExp(0d);
        appUser.setVip(false);
        // 设置初始昵称
        appUser.setNickname("盼" + RandomUtil.getRandomNumStr(5));
        appUser.setUserName(appUser.getNickname());

        appUser.setAccountIntegral(1d);
        appUser.setTotalIntegral(1d);
        // 保存
        dao.insertSelective(appUser);
        //送积分了
        //giftService.sendForRegister(appUser.getUid());
        return appUser;
    }
}
